package com.jzoffer;

import java.util.*;

/*
 *   使用双栈实现队列，实现队列的基本操作
 *  add(offer) 添加操作 队尾
 *  peek 查看队头元素
 *  poll(remove) 删除队头元素
 * */
public class DoubleStack {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 操作总数
        int N = scanner.nextInt();

        // 存储各种操作
        List<Integer>  res = new ArrayList<>();
        // 创建两个栈，模拟队列操作
        // 进
        Stack<Integer> in = new Stack<>();
        // 出
        Stack<Integer> out = new Stack<>();
        for (int i = 0; i < N+1; i++) {
            String operation = scanner.nextLine();
            int value = getRes(in,out,operation);
            if(value != Integer.MIN_VALUE){
                res.add(value);
            }
        }

        for(int i = 0;i < res.size();i++){
            System.out.println(res.get(i));
        }


    }

    private static int getRes(Stack<Integer> in,Stack<Integer> out ,String operations) {
        List<Integer> list = new ArrayList<>();
        if (operations == null || operations.length() < 1) {
            return Integer.MIN_VALUE;
        }

        // 遍历操作，选择对应操作

        String op = operations;
        // 有空格 表示添加元素
        if (op.contains(" ")) {
            String[] split = op.split(" ");
            String op1 = split[0];
            String value = split[1];
            if (!(Integer.valueOf(value) instanceof Integer)) {
                throw new RuntimeException("增加的不是数字，请重新输入！");
            }
            if (op1.equals("add")) {
                add(in, Integer.parseInt(value));
            } else if (op1.equals("offer")) {
                offer(in, Integer.parseInt(value));
            }

        } else if (op.equals("poll") || op.equals("remove")) {
            if (op.equals("poll")) {
                poll(in, out);
            } else {
                remove(in, out);
            }

        } else if (op.equals("peek")) {
            int value = peek(in, out);
            if (value != Integer.MIN_VALUE) {
                return value;
            }
        }

        return Integer.MIN_VALUE;
    }

    // 添加元素到队尾
    private static void add(Stack<Integer> in, int value) {
        offer(in, value);
    }

    // 添加元素到队尾
    private static void offer(Stack<Integer> in, int value) {
        in.push(value);
    }

    // 取出队头元素值
    private static int peek(Stack<Integer> in, Stack<Integer> out) {
        if (in.isEmpty() && out.isEmpty()) {
            return Integer.MIN_VALUE;
        } else {

            while (!in.isEmpty()) {
                out.push(in.pop());
            }
            int value = out.peek();
            while (!out.isEmpty()) {
                in.push(out.pop());
            }
            return value;
        }
    }

    // 取出队头元素
    private static void poll(Stack<Integer> in, Stack<Integer> out) {
        if (in.isEmpty() && out.isEmpty()) {
            return;
        } else {
            while (!in.isEmpty()) {
                out.push(in.pop());
            }
            out.pop();
            if (out.isEmpty()) {
                return;
            }
            while (!out.isEmpty()) {
                in.push(out.pop());
            }
        }
    }

    private static void remove(Stack<Integer> in, Stack<Integer> out) {
        if (in.isEmpty() && out.isEmpty()) {
            throw new RuntimeException("未添加元素,无法删除队头，请重新输入！");
        } else {
            poll(in, out);
        }
    }

}
